package com.yycx.module.bus.provider.service.hy.impl;

import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.constants.CommonConstants;
import com.yycx.common.mybatis.base.service.impl.BaseServiceImpl;
import com.yycx.common.mybatis.model.ResultBody;
import com.yycx.common.mybatis.query.CriteriaQuery;
import com.yycx.common.mybatis.query.CriteriaSave;
import com.yycx.common.mybatis.query.CriteriaUpdate;
import com.yycx.common.security.OpenHelper;
import com.yycx.module.bus.client.entity.BusDept;
import com.yycx.module.bus.client.entity.hy.BusPolicyFile;
import com.yycx.module.bus.provider.mapper.hy.BusPolicyFileMapper;
import com.yycx.module.bus.provider.service.hy.BusPolicyFileService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * 政策文件表接口实现类
 *
 * @author flyme
 * @date 2023-07-14
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BusPolicyFileServiceImpl extends BaseServiceImpl<BusPolicyFileMapper, BusPolicyFile> implements BusPolicyFileService {

    @Override
    public ResultBody beforeAdd(CriteriaSave cs, BusPolicyFile bpf, EntityMap extra) {
        bpf.setStatus(CommonConstants.ENABLED);
        bpf.setUserId(OpenHelper.getUserId());
        return ResultBody.ok();
    }

    @Override
    public ResultBody beforeEdit(CriteriaUpdate<BusPolicyFile> cu, BusPolicyFile busPolicyFile, EntityMap extra) {
        Date endDate = busPolicyFile.getEndDate();
        if (FlymeUtils.isNotEmpty(endDate)) {
            //结束日期不为空时代表是禁用操作
            busPolicyFile.setStatus(CommonConstants.DISABLED);
        } else {
            busPolicyFile.setStatus(CommonConstants.ENABLED);
        }
        return super.beforeEdit(cu, busPolicyFile, extra);
    }

    @Override
    @Transactional(propagation = Propagation.NOT_SUPPORTED, readOnly = true)
    public ResultBody beforePageList(CriteriaQuery<BusPolicyFile> cq, BusPolicyFile bpf, EntityMap requestMap) {
        cq.like(BusPolicyFile.class,"policyTitle");
        cq.select(BusPolicyFile.class, "*");
        cq.addSelect(BusDept.class, "deptName");
        cq.orderByDesc("bpf.createTime");
        cq.createJoin(BusDept.class);
        return ResultBody.ok();
    }
}
